package org.wheres.core;

import java.util.Map;

import org.jivesoftware.smack.AccountManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.Roster.SubscriptionMode;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Packet;
import org.wheres.core.contactlist.InvalidContactNameException;
import org.wheres.core.thread.ConnectionThread;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class Communicator extends Service implements CommunicationManager {
	
	private final IBinder CommunicatorBinderIstance = new CommunicatorBinder();
	private XMPPConnection connection;
	private ConnectionConfiguration config;
	private Roster roster;
	private String server = "10.0.2.2";
	private int port = 5222;
	
	public class CommunicatorBinder extends Binder{
		
		public Communicator getService(){
			return Communicator.this;
		}
		
	}
	
	
	@Override
	public void onCreate() {		
		
		this.config = new ConnectionConfiguration(server, port);
		this.config.setSASLAuthenticationEnabled(false);
		this.connection = new XMPPConnection(config);
	}
	
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		
		ConnectionThread connectionThread = new ConnectionThread(this.connection);
		connectionThread.start();
		return START_STICKY;
	}
	
	
	

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return CommunicatorBinderIstance;
	}
	
	
	
	@Override
	public void sendMessage(String message, String to) {
		// TODO Auto-generated method stub
		
	}




	@Override
	public void addContactToServer(String contact)
			throws InvalidContactNameException, XMPPException {
		// TODO Auto-generated method stub
		
	}




	@Override
	public void removeContactFromServer(String contact)
			throws InvalidContactNameException, XMPPException {
		// TODO Auto-generated method stub
		
	}




	@Override
	public void startConnection(String userName, String userPassword) 
			throws XMPPException {
		if(this.connection.isConnected())
			connection.login(userName, userPassword);		
	}




	@Override
	public void createAccount(String userName, String userPassword, Map<String, String> UserProp) 
			throws XMPPException {
		
		if(this.connection.isConnected()){
			AccountManager accountManager = new AccountManager(this.connection);
			accountManager.createAccount(userName, userPassword, UserProp);
		}
		
	}

	

	@Override
	public void disconnect() {
		this.connection.disconnect();
		
	}




	@Override
	public void updateRoster() {
		// TODO Auto-generated method stub
		
	}
	
	
	@Override
	public Roster getRoster(RosterListener rosterListener) {
		if(this.connection.isConnected()){
			this.roster = this.connection.getRoster();
			this.roster.setSubscriptionMode(SubscriptionMode.accept_all);
			this.roster.addRosterListener(rosterListener);
			return this.roster;
		}
		return null;
	}
	


	@Override
	public void processPacket(Packet arg0) {
		// TODO Auto-generated method stub
		
	}


}
